<?php 

	function getBookOpf($link) {

		//Get file and assign to object
		$xmlObject = @simplexml_load_file($link.'/content.opf');
		if ($xmlObject) return array('object' => $xmlObject, 'isOEBPS' => false);

		else $xmlObject = @simplexml_load_file($link.'/OEBPS/content.opf');
			if ($xmlObject) return array('object' => $xmlObject, 'isOEBPS' => true);

			else return -1;
	}
//================================================================================

	function getBookToc($link) {

		//Get file and assign to object
		$xmlObject = @simplexml_load_file($link.'/toc.ncx');
		if ($xmlObject) return array('object' => $xmlObject, 'isOEBPS' => false);

		else $xmlObject = @simplexml_load_file($link.'/OEBPS/toc.ncx');
			if ($xmlObject) return array('object' => $xmlObject, 'isOEBPS' => true);
			
			else return -1;
	}
//================================================================================

	function getCover($bookId) {
		$link = base_url().'assets/resource/'.$bookId;
		$temp = getBookOpf($link);

		$xmlObject = $temp['object'];

		$bookCover = ''; //Store book cover and link to chapter

		if ($xmlObject != -1) //Check error from load file proccessing.
		{
			foreach ($xmlObject->manifest->children() as $child) {
				$attr = $child->attributes();
				if (($attr['id'] == 'cover' || $attr['id'] == 'cover-image') && $attr['media-type'] == 'image/jpeg') {
					if(!$temp['isOEBPS'])
						$bookCover = $link . '/' . $attr['href'];
					else $bookCover = $link . '/OEBPS/' . $attr['href'];
					break;
				}
			}
			if($bookCover == '') {
				$CI = get_instance();
				$CI->load->model('model_books');
				$book = $CI->model_books->getBook($bookId);

				$letter = mb_substr($book['name_document'], 0, 1);
				if( strtoupper($letter) == "Đ") 
					$letter = 'D';
				if( strtoupper($letter) == "Ô" || strtoupper($letter) == "Ơ" ||
					strtoupper($letter) == "Ố" || strtoupper($letter) == "Ớ" ||
					strtoupper($letter) == "Ổ" || strtoupper($letter) == "Ở" ||
					strtoupper($letter) == "Ỗ" || strtoupper($letter) == "Ỡ" ||
					strtoupper($letter) == "Ồ" || strtoupper($letter) == "Ờ" ||
					strtoupper($letter) == "Ộ" || strtoupper($letter) == "Ợ" ||
					strtoupper($letter) == "Ó" || strtoupper($letter) == "Ò" ||
					strtoupper($letter) == "Õ" || strtoupper($letter) == "Ọ" ||
					strtoupper($letter) == "Ỏ") 
					$letter = 'O';
				if( strtoupper($letter) == "Ê" || strtoupper($letter) == "Ế" ||
					strtoupper($letter) == "Ể" || strtoupper($letter) == "Ề" ||
					strtoupper($letter) == "Ệ" || strtoupper($letter) == "Ễ" ||
					strtoupper($letter) == "É" || strtoupper($letter) == "È" ||
					strtoupper($letter) == "Ẽ" || strtoupper($letter) == "Ẹ" ||
					strtoupper($letter) == "Ẻ") 
					$letter = 'E';
				if( strtoupper($letter) == "Ư" || strtoupper($letter) == "Ứ" ||
					strtoupper($letter) == "Ừ" || strtoupper($letter) == "Ử" ||
					strtoupper($letter) == "Ữ" || strtoupper($letter) == "Ự" ||
					strtoupper($letter) == "Ú" || strtoupper($letter) == "Ù" ||
					strtoupper($letter) == "Ũ" || strtoupper($letter) == "Ủ" ||
					strtoupper($letter) == "Ụ") 
					$letter = 'U';
				if( strtoupper($letter) == "Í" || strtoupper($letter) == "Ì" ||
					strtoupper($letter) == "Ỉ" || strtoupper($letter) == "Ị" ||
					strtoupper($letter) == "Ĩ") 
					$letter = 'I';
				if( strtoupper($letter) == "Ý" || strtoupper($letter) == "Ỳ" ||
					strtoupper($letter) == "Ỷ" || strtoupper($letter) == "Ỵ" ||
					strtoupper($letter) == "Ỹ") 
					$letter = 'Y';

				$bookCover = base_url() . 'assets/img/cover/cover'.$letter.'.jpg';
			}
			return $bookCover;
		}
		else return 'ERROR-BOOK';
	}
//================================================================================

	function getTableContent($bookId) {
		$link = base_url().'assets/resource/'.$bookId;
		$arrTableContent = array();

		$tempOpf = getBookOpf($link);

		if ( $tempOpf != - 1 ) 
		{
			$opf = $tempOpf['object'];
			$number = 1;
			
			foreach ($opf->spine->children() as $itemref) 
			{
				foreach ($opf->manifest->children() as $item) 
				{	
					if ( (string)$itemref['idref'] == (string)$item['id'] ) 
					{
						if( (string)$item['id'] == 'titlepage')
							$node['text'] = 'Trang bìa';
						else {
							$node['text'] = 'Chương '.$number;
							$number++;
						}
						$node['id'] = $item['id'];
						$node['link'] = $item['href'];
						array_push($arrTableContent, $node);
						break;
					}
				}
			}
			return $arrTableContent;
		}
		else return 'ERRORBOOK';
	}

	// Clone nodes from content to file
	function cloneNode($node, $dom) {
		$el = $dom->createElement($node->nodeName);

		foreach($node->attributes as $val) {
			if($val->nodeName == "src") {
				$pos = strpos($val->value, 'resource/');
				$l = substr($val->value, $pos);
					$pos = strpos($l, '/');
					$l = substr($l, $pos + 1);
						$pos = strpos($l, '/');
						$l = substr($l, $pos + 1);
				$node->setAttribute("src", $l);
			}
			$el->setAttribute($val->nodeName, $val->value);
		}
		if (!$node->childNodes) return $node;
		foreach($node->childNodes as $child) 
		{
			if($child->nodeName == "#text")
				$el->appendChild($dom->createTextNode($child->nodeValue));
			
			else $el->appendChild(cloneNode($child, $dom));
		}
		return $el;
	}

 ?>